package com.mymarkdown.app.functions.system;

import com.mymarkdown.app.common.annotation.SystemConfigChangeHandler;
import com.mymarkdown.app.common.enums.SystemSettingChangeEventEnum;
import com.mymarkdown.app.interfaces.HandlerSystemConfigChange;
import com.mymarkdown.app.model.UserSettingInfo;
import com.mymarkdown.app.websocket.enums.MessageTypeEnum;
import com.mymarkdown.app.websocket.interfaces.WebSocketSendMessage;
import com.mymarkdown.app.websocket.message.MessagePojo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;

/**
 * @author zenglun
 * @date 2021/12/15 5:55 下午
 * @description: 系统配置自动保存的值发生改变时的操作
 */
@Slf4j
@Component
@SystemConfigChangeHandler(SystemSettingChangeEventEnum.CHANGE_AUTO_SAVE)
public class AutoSaveSystemConfigChangeHandlerImpl implements HandlerSystemConfigChange {

    @Autowired
    private WebSocketSendMessage webSocketSendMessage;

    @Override
    public void handler(Object originalVal, Object newVal, Field field,
                        UserSettingInfo originalSettingInfo, UserSettingInfo userSettingInfo) throws Exception {
        // 设置自动保存的值
        webSocketSendMessage.sendMessage(MessagePojo.empty().setMessageType(MessageTypeEnum.SET_AUTO_SAVE_CONF.getMessageType()).setBody(newVal+""));
    }

    @Override
    public void error(Exception exception) throws Exception {
        webSocketSendMessage.sendMessage(MessagePojo.empty().setMessageType(MessageTypeEnum.TISP_MSG.getMessageType()).setBody("改变自动保存配置失败!"));
    }
}
